package RSA;

import java.math.BigInteger;

public class RSATest {

    private static final RSA rsa = new RSA();

    private static class RSATestData {
        // 公钥是(e,n)，私钥是(d,n)
        private final BigInteger e;
        private final BigInteger d;
        private final BigInteger n;

        private final BigInteger plaintext;
        private final BigInteger ciphertext;


        private RSATestData(String e, String d, String n,
                            String plaintext, String ciphertext) {
            this.e = new BigInteger(e);
            this.d = new BigInteger(d);
            this.n = new BigInteger(n);
            this.plaintext = new BigInteger(plaintext);
            this.ciphertext = new BigInteger(ciphertext);
        }

        public BigInteger getE() {
            return e;
        }

        public BigInteger getD() {
            return d;
        }

        public BigInteger getN() {
            return n;
        }

        public BigInteger getPlaintext() {
            return plaintext;
        }

        public BigInteger getCiphertext() {
            return ciphertext;
        }
    }

    public static void main(String[] args) {
        primaryTest();
    }


    private static void primaryTest() {
        RSATestData[] datas = new RSATestData[]{
                new RSATestData("7", "23", "187", "88", "11"),
                new RSATestData("11", "5891", "11023", "3314", "10260"),
                new RSATestData("265894244486361609418672981644651529873020297744073959598497028329718567562205066233674093994926761700108625121413108075823180240338283127587432244503964144088437329787584717834950463076315349549075540138745002899585515973689539637773540929626639785728515456905360319531546911834467244380602736290039189017347",
                        "267288970893450600534305531847326374025164891935069321683040616774132953708877449911136999134366885172717941036135175160061712625744337821314706981689344952150552224724186701805432236678436429441199613272929812856169464912529086033683210111980779383273199505949067799565486890404374130976492470416775775712171",
                        "294397307361584321829785887095539725649616857669708368088424130611738002239260877748112169821706510690509498746846738964246170261309806471395358341942587767996566494129484965550113824179689894205620357132135411739903957285933743517858107050688110730401728803293799797577288234353034911919509320526582135202311",
                        "1122334455667788",
                        "71860032154443168513685152570966635952098135309451679709998248703130156773370334104776883586352892879997808224795459633678367264467288086300880630764205357666097233102939045352472097661096284910465401038464173200514448495414921305878475668702805050346013130564721947331500481523796112589075775407109552347709")
        };

        boolean flag = true;

        for (RSATestData data : datas) {
            BigInteger encrypt = rsa.encrypt(data.getPlaintext(), data.getE(), data.getN());
            if (!encrypt.equals(data.getCiphertext())) {
                flag = false;
                break;
            }
            BigInteger decrypt = rsa.decrypt(encrypt, data.getD(), data.getN());
            if (!decrypt.equals(data.getPlaintext())) {
                flag = false;
                break;
            }
        }

        if (flag) {
            System.out.println("RSATest.primaryTest：测试通过！");
        } else {
            System.out.println("RSATest.primaryTest：测试不通过！");
        }
    }
}
